/**
 * 
 */
package galaxy.drawer.action;

import galaxy.drawer.Context;
import galaxy.drawer.drawable.Line;
import galaxy.math.AffineTransform;
import processing.core.PVector;

public class DrawAction implements Action
{
	private float angle;
	private float length;
	
	public DrawAction(float angle, float length)
	{
		this.angle = angle;
		this.length = length;
	}
	
	public DrawAction()
	{}
	
	public DrawAction setAngle(float angle)
	{
		this.angle = angle;
		return this;
	}
	
	public DrawAction setLength(float length)
	{
		this.length = length;
		return this;
	}
	
	public Context execute(Context c)
	{
		if (angle != 0) {
			c.angle += angle;
		}
		
		if (length != 0) {
			PVector direction = getDirection(c.angle);
			PVector end = PVector.add(c.pos, direction);
			c.drawables.add(new Line(c.pos.get(), end.get()));
			c.setPosition(end);
		}
		
		return c;
	}
	
	public PVector getDirection(float angle)
	{
		return rotate(new PVector(length, 0), angle);
	}
	
	public PVector rotate(PVector p, float angle)
	{
		return AffineTransform.rotate(angle).apply(p);
	}
}